package top.ijiujiu.component;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import top.ijiujiu.entity.SysUser;
import top.ijiujiu.service.ISysUserService;

import java.util.Date;

/**
 * InitUserComponent
 *
 * @Author 神秘的黄瓜
 * @Version 1.0
 * @Date 2020-06-22 14:48
 */
@Component
public class InitUserComponent implements CommandLineRunner {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public void run(String... args) throws Exception {
        SysUser user = this.userService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getAccount, "admin"));
        if (ObjectUtils.isEmpty(user)) {
            user = new SysUser();
            user.setAccount("admin");
            user.setPwd(this.passwordEncoder.encode("123456"));
            user.setName("admin");
            user.setSex(1);
            user.setAge(100);
            this.userService.save(user);
        }
    }
}
